<template>
	<view>
		<mvBar :mysNavConfig="mysNavConfig" @onLeftClick="onLeftClick"></mvBar>
		<!-- 消息提示 -->
		<u-toast ref="uToast"></u-toast>
		<!-- 批量修改的设备和测点名称 -->
		<view class="zname" v-for="(item,index) in indexdata" :key="item.PointFeatureRealId">
			<view>{{item.EquipmentName+'-'+item.PointName}}</view>
		</view>
		<!-- 指标名称和阈值设置 -->
		<view class="uniview">
			<uni-card padding="10px 0">
				<template v-slot:title>
					<view class="title">
						<view class="text">
							{{indexname}}{{indexunit=='ma'?'(%)':indexunit==''?'(无)':'('+indexunit+')'}}
						</view>
					</view>
				</template>
				<view style="padding: 0px 10px;" v-if="indexunit=='°'||indexunit=='ma'">
					<!-- 当单位是°或者ma的时候,表示为电量和倾角 -->
					<u-form labelPosition=" left">
						<view class="inputviews">
							<view class="inputview">
								<u-input type="digit" inputAlign='center' v-model="yj" placeholder="下限值"
									border="bottom">
								</u-input>
							</view>
							<view class="texts">{{'<'}} </view>
							<view class="inpvie">一级报警</view>
							<view class="texts"> {{'<'}}</view>
							<view class="inputview">
								<u-input type="digit" inputAlign='center' v-model="unusual" placeholder="上限值"
									border="bottom">
								</u-input>
							</view>
						</view>
						<view class="inputviews">
							<view class="inputview">
								<u-input type="digit" inputAlign='center' v-model="bj" placeholder="下限值"
									border="bottom">
								</u-input>
							</view>
							<view class="texts">{{'<'}} </view>
							<view class="inpvie">二级报警</view>
							<view class="texts"> {{'<'}}</view>
							<view class="inputview">
								<u-input type="digit" inputAlign='center' v-model="warning" placeholder="上限值"
									border="bottom">
								</u-input>
							</view>
						</view>
						<view class="inputviews">
							<view class="inputview">
								<u-input type="digit" inputAlign='center' v-model="gb" placeholder="下限值"
									border="bottom">
								</u-input>
							</view>
							<view class="texts">{{'<'}} </view>
							<view class="inpvie">三级报警</view>
							<view class="texts"> {{'<'}}</view>
							<view class="inputview">
								<u-input type="digit" inputAlign='center' v-model="highRisk" placeholder="上限值"
									border="bottom">
								</u-input>
							</view>
						</view>
					</u-form>
				</view>
				<view style="padding: 0px 10px;" v-else>
					<u-form labelPosition="left">
						<u-form-item label="最小值" label-width="75" borderBottom>
							<u-input type="digit" v-model="min" placeholder="请输入最小值" border="none">
							</u-input>
						</u-form-item>
						<u-form-item label="一级报警" label-width="75" borderBottom>
							<u-input type="digit" v-model="unusual" placeholder="请输入一级报警值" border="none">
							</u-input>
						</u-form-item>
						<u-form-item label="二级报警" label-width="75" borderBottom>
							<u-input type="digit" v-model="warning" placeholder="请输入二级报警值" border="none">
							</u-input>
						</u-form-item>
						<u-form-item label="三级报警" label-width="75" borderBottom>
							<u-input type="digit" v-model="highRisk" placeholder="请输入三级报警值" border="none">
							</u-input>
						</u-form-item>
					</u-form>
				</view>
			</uni-card>
		</view>
		<view style="width: 90%; margin:10% auto;">
			<u-button shape='circle' type="primary" text="确定" @click="alter"></u-button>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				mysNavConfig: {
					/* 占位开启  */
					navPadding: true,
					/* 背景 */
					bgColor: "#2979FF",
					/* 标题 （屏幕中心居中 两边图标中心居中使用slot center1） */
					navTitle: {
						text: "批量修改阈值",
						color: "#ffffff",
						fontSize: "18px", // px upx rpx
						fontWeight: "500", // 100 - 700
					},
					isCustomFn: true,
					btnType: "type2",
					type2Config: {
						// 左图标
						leftPath: "/static/img/back.png",
						// 右图标
						rightPath: "/static/img/home.png",
						// 圆角
						radius: "40rpx",
					}
				},
				userinfo: '',
				indexdata: '',
				indexunit: '',
				indexname: '',
				min: '',
				unusual: '',
				warning: '',
				highRisk: '',
				yj: '',
				bj: '',
				gb: '',
			}
		},
		onLoad() {
			this.userinfo = uni.getStorageSync('userinfo');
			this.indexdata = this.$store.state.ContrastTrendChart;
			this.indexunit = this.indexdata[0].PointFeatureUnit;
			this.indexname = this.indexdata[0].PointFeatureName;
			this.$store.commit('alterContrastTrendChart', '');
		},
		methods: {
			//返回到指标对比页面
			onLeftClick() {
				this.$store.commit('alterContrastTrendChart', this.indexdata);
				uni.redirectTo({
					url: '/pages/equipment/ContrastTrendChart'
				})
			},
			//修改
			alter() {
				if (this.unusual === '' || this.warning === '' || this.highRisk === '') {
					this.$utils.showToast(this, this.indexname + "阈值不能为空！", 'warning');
					return
				}
				if (isNaN(Number(this.unusual)) || isNaN(Number(this.warning)) || isNaN(Number(this
						.highRisk))) {
					this.$utils.showToast(this, "请输入数字！", 'warning');
					return
				}
				if (this.indexunit == '°' || this.indexunit == 'ma') {
					if (this.yj === '' || this.bj === '' || this.gb === '') {
						this.$utils.showToast(this, this.indexname + "阈值不能为空", 'warning');
						return;
					}
					if (isNaN(Number(this.yj)) || isNaN(Number(this.bj)) || isNaN(Number(this
							.gb))) {
						this.$utils.showToast(this, "请输入数字！", 'warning');
						return;
					}
					if (Number(this.yj) > Number(this.unusual) || Number(this.unusual) >
						Number(this.bj) || Number(this
							.bj) > Number(this.warning) || Number(this.warning) > Number(this
							.gb) ||
						Number(this.gb) > Number(this.highRisk)) {
						this.$utils.showToast(this, this.indexname + "阈值设置错误", 'warning');
						return;
					}
					this.min = this.yj + '&' + this.bj + '&' + this.gb;
				} else {
					if (this.min === '') {
						this.$utils.showToast(this, this.indexname + "阈值不能为空", 'warning');
						return;
					}
					if (isNaN(Number(this.min))) {
						this.$utils.showToast(this, "请输入数字！", 'warning');
						return;
					}
					if (Number(this.min) > Number(this.unusual) || Number(this.unusual) > Number(this.warning) || Number(
							this.warning) >
						Number(this.highRisk)) {
						this.$utils.showToast(this, this.indexname + "阈值设置错误！", 'warning');
						return
					}
				}
				var indexlistarr = []
				for (let item of this.indexdata) {
					indexlistarr.push({
						pointFeatureRealId: item.PointFeatureRealId,
						min: this.min,
						unusual: this.unusual,
						warning: this.warning,
						highRisk: this.highRisk,
					})
				}
				uni.showLoading({
					title: "修改中...",
					mask: true,
				});
				uni.request({
					url: getApp().globalData.serverAddress + this.$uri.Uri_batchchangeindex,
					data: JSON.stringify(indexlistarr),
					dataType: "json",
					method: "POST",
					success: (res) => {
						if (res.statusCode !== 200) {
							this.$utils.showToast(this, '服务器错误,请联系技术支持人员!', 'error');
							return
						}
						if (res.data.code == 0) {
							this.$store.commit('alterContrastTrendChart', this.indexdata);
							this.$utils.showToast(this, '修改成功！', 'success', 2,
								'/pages/equipment/ContrastTrendChart');
						} else {
							this.$utils.showToast(this, res.data.msg, 'error');
						}
					},
					fail() {
						this.$utils.showToast(this, "修改失败！", 'error');
					},
					complete() {
						uni.hideLoading();
					}
				})
			},
		}
	};
</script>

<style lang="scss">
	.text {
		width: 73%;
		display: inline-block;
	}

	.uniview {
		margin: 20px;
		padding: 0px 10px;
		background-color: #ffffff;
		box-shadow: 0px 0px 3px 1px rgba(212, 214, 220, 1.0);
		border-radius: 3%;
	}

	.title {
		padding: 0px 10px;
		line-height: 46px;
		height: 46px;
		border-bottom: 0.5px solid #e0e1e2;
	}


	.zname {
		text-align: center;
		color: #3c9cff;
		margin: 10px auto;
		height: 30px;
		font-size: 18px;
		font-weight: bold;
		line-height: 30px;
		padding-bottom: 5px;
	}

	.inputviews view {
		display: inline-block;
	}

	.inputview {
		width: 30%;
	}

	.inpvie {
		height: 45px;
		line-height: 45px;
		width: 30%;
		text-align: center;
		font-size: 16px;
	}

	.texts {
		font-size: 20px;
	}
</style>